﻿using SqlSugar;
using System.Linq.Expressions;

namespace S_Examine.Infrastructure
{
	public class BaseRepository<T> : IRepository<T> where T : class, new()
	{
		private readonly ISqlSugarClient _client;

		public BaseRepository(ISqlSugarClient client)
		{
			_client = client;
		}

		public async Task<int> CreateAsync(T entity)
		{
			return await _client.Insertable(entity).ExecuteCommandAsync();
		}

		public async Task<int> CreateAsyncRangeAsync(List<T> values)
		{
			return await _client.Insertable(values).ExecuteCommandAsync();
		}

		public async Task<int> DeleteAsync(T entity)
		{
			return await _client.Deleteable(entity).ExecuteCommandAsync();
		}

		public async Task<int> BatchDeleteAsync(List<T> list)
		{
			return await _client.Deleteable(list).ExecuteCommandAsync();
		}

		public async Task<List<T>> GetAllAsync()
		{
			return await _client.Queryable<T>().ToListAsync();
		}

		public async Task<List<T>> GetAsync(Expression<Func<T, bool>> expre)
		{
			return await _client.Queryable<T>().Where(expre).ToListAsync();
		}

		public async Task<T> GetByFuncAsync(Expression<Func<T, bool>> expre)
		{
			return await _client.Queryable<T>().FirstAsync(expre);
		}

		public async Task<T> GetByIdAsync(int id)
		{
			return await _client.Queryable<T>().InSingleAsync(id);
		}

		public async Task<int> UpdateAsync(T entity)
		{
			return await _client.Updateable(entity).ExecuteCommandAsync();
		}

		public async Task<int> UpdateRangeAsync(List<T> values)
		{
			return await _client.Updateable(values).ExecuteCommandAsync();
		}
	}
}
